﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.Model;
using Wsitech.BLL;
using System.IO;
public partial class News_NewsDetail : System.Web.UI.Page
{
    private readonly string tid = HttpContext.Current.Request["tid"] ?? "";

    /// <summary>
    /// 文字信息
    /// </summary>
    public TextInfo Info
    {
        get
        {
            return TextInfoBLL.Instance.GetDetail(tid);
        }
    }

    /// <summary>
    /// 获取栏目名称
    /// </summary>
    /// <returns></returns>
    protected string GetSiteMapName()
    {
        if (Info != null)
        {
            return SiteMapInfoBLL.Instance.GetSiteMapName(Info.SitemapId);
        }
        return "";
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Info != null)
            {
                initNews();
            }
        }
    }

    private void initNews()
    {
        SiteMapInfo sitemap = SiteMapInfoBLL.Instance.GetDetail(Info.SitemapId);
        if (sitemap.FrontUrl != "")
        {
            //上一篇和下一篇
            TextInfo prev = TextInfoBLL.Instance.GetPrevRecord(Info);
            if (prev != null)
            {
                hlPrev.Text = "上一篇：" + prev.Title;
                hlPrev.NavigateUrl = "NewsDetail.aspx?tid=" + prev.TextInfoId;
            }
            else
            {
                hlPrev.Visible = false;
            }
            TextInfo next = TextInfoBLL.Instance.GetNextRecord(Info);
            if (next != null)
            {
                hlNext.Text = "下一篇：" + next.Title;
                hlNext.NavigateUrl = "NewsDetail.aspx?tid=" + next.TextInfoId;
            }
            else
            {
                hlNext.Visible = false;
            }

            //返回
            if (sitemap != null)
            {
                hlBack.Text = "返回" + sitemap.SitemapName;
                hlBack.NavigateUrl = string.Format("{0}?sitemapid={1}", sitemap.FrontUrl, sitemap.SitemapId);
            }
        }
        else
        {
            controlPanel.Visible = false;
        }

        //计数+1
        TextInfo info = TextInfoBLL.Instance.GetDetail(tid);
        info.ViewCount += 1;
        TextInfoBLL.Instance.Update(info);

    }

    //下载
    protected void ResourceDownload(object sender, EventArgs e)
    {
        string filepath = Server.MapPath(Info.SourcePath);
        if (File.Exists(filepath))
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(filepath);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ClearHeaders();
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filepath), System.Text.Encoding.UTF8));
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
            System.Web.HttpContext.Current.Response.WriteFile(fi.FullName);
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }
    }
}